﻿var img = new Image;
//var img2 = new Image;

//var btnPlay = {
//    sX: 974,
//    sY: 7,
//    sW: 91,
//    sH: 39,
//    dX: 463,
//    dY: 41,
//    dW: 91,
//    dH: 39
//    };


function ConferenceRoom() {

}

ConferenceRoom.Initialize = function () {
    this.context = document.getElementById("ConferenceCanvas").getContext("2d");

    var canvas = $('#ConferenceCanvas');

    var canvasPosition = {
        x: canvas.offset().left,
        y: canvas.offset().top
    };

    var doubleClickThreshold = 250;  //ms
    var lastClick = 0;
    var isDragging = false;
    var isDoubleClick = false;
    var mouseIsDown = false;

    canvas.click(function (e) {
        var thisClick = new Date().getTime();
        var isDoubleClick = thisClick - lastClick < doubleClickThreshold;
        lastClick = thisClick;

        var mouse = {
            x: e.pageX - canvasPosition.x,
            y: e.pageY - canvasPosition.y
        }

        HandleClick(mouse.x, mouse.y);

        if (isDoubleClick == true) {
            alert('X:' + mouse.x + ' Y:' + mouse.y);
        }
    });

}

ConferenceRoom.Draw = function () {
    this.context.drawImage(img, 0, 0); //img, X, Y
   // this.context.drawImage(img2, btnPlay.sX, btnPlay.sY, btnPlay.sW, btnPlay.sH, btnPlay.dX, btnPlay.dY, btnPlay.dW, btnPlay.dH); //img, sX, sY, sW, sH, dX, dY, dW, dH
}

ConferenceRoom.Start = function () {
    var imgLoaded = false;
    //var img2Loaded = false
    img.onload = function () {
        imgLoaded = true;
        if (imgLoaded == true) {
            ConferenceRoom.Initialize();
            ConferenceRoom.Draw();
        }
    }
    //img2.onload = function () {
    //    img2Loaded = true;
    //    if (imgLoaded == true) {
    //        ConferenceRoom.Initialize();
    //        ConferenceRoom.Draw();
    //    }
    //}
    var imgurl = window.location.protocol + '//' + window.location.host + '/Images/SpriteSheets/010.bmp';
    //var imgurl2 = window.location.protocol + '//' + window.location.host + '/Images/SpriteSheets/009.png';
    img.src = imgurl;
    //img2.src = imgurl2;

}
//End Login Functions


//Begin Service Calls
function AuthenticateUser() {

    var varType = "POST";
    var varUrl = "services/AuthenticationService.svc/Login";
    var varData = '{"UserName": "' + $('#UserName').val() + '", "Password": "' + $('#Password').val() + '"}';
    var varContentType = "application/json; charset=utf-8";
    var varDataType = "json";
    var varProcessData = true;
    
    $.ajax({
        type: varType, //GET or POST or PUT or DELETE verb
        url: varUrl, // Location of the service
        data: varData, //Data sent to server
        contentType: varContentType, // content type sent to server
        dataType: varDataType, //Expected data format from server
        processdata: varProcessData, //True or False
        success: function (msg) {//On Successfull service call
            ServiceSucceeded(msg);
        },
        error: ServiceFailed// When Service call fails
    });
}

function ServiceSucceeded(result) {//When service call is sucessful
    if (result.LoginResult.ID > 0) {
        window.location = 'ConferenceRoom.aspx?ID=' + result.LoginResult.GUID;
    } else {
        var ErrMsgs = 'Error Logging in: \n';
        for (i = 0; i < result.LoginResult.ErrorMessages.length; i++) {
            ErrMsgs += ' ' + result.LoginResult.ErrorMessages[i] + ' \n';
        }

        alert(ErrMsgs);
    }
}
function ServiceFailed(result) {
    alert('Login Service call failed: ' + result.status + '' + result.statusText);
}


function HandleClick(x, y) {
    //Play Button
    //if (x >= btnPlay.dX && x <= (btnPlay.dX + btnPlay.dW) && y >= btnPlay.dY && y <= (btnPlay.dY + btnPlay.dH)) {
    //    alert('Play Clicked');
    //}
}

